package com.styytech.dadabao.ui.my.accountset;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.loopj.android.http.RequestParams;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.api.requestresult.RegistResult;
import com.styytech.dadabao.api.response.AbstractResponseResult;
import com.styytech.dadabao.api.templates.HttpRequest;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.constant.ConstantsServerUrl;
import com.styytech.dadabao.exception.ArgumentsException;
import com.styytech.dadabao.exception.RequestException;
import com.styytech.dadabao.utils.CommonUtils;
import com.styytech.dadabao.utils.IdCheckUtils;
import com.styytech.dadabao.utils.SpUser;

import java.text.ParseException;

/**
 * 实名认证
 */
@ContentView(R.layout.activity_name_auth)
public class AuthNameActivity extends BaseActivity {

    /*真是姓名*/
    @ViewInject(R.id.et_name)
    TextView et_name;
    /*性别男*/
    @ViewInject(R.id.iv_male)
    ImageView iv_male;
    /*性别女*/
    @ViewInject(R.id.iv_woman)
    ImageView iv_woman;
    /*身份证号码*/
    @ViewInject(R.id.et_identity_card)
    TextView et_identity_card;
    @ViewInject(R.id.llyt_jobnum)
    LinearLayout llyt_jobnum;
    /*工号*/
    @ViewInject(R.id.et_job_number)
    EditText et_job_number;
    /*验证码*/
    @ViewInject(R.id.et_auth_code)
    EditText et_auth_code;
    /*获取验证码*/
    @ViewInject(R.id.tv_auth_code)
    TextView tv_auth_code;
    /*提交审核*/
    @ViewInject(R.id.bt_submit)
    Button bt_submit;


    /*上下文*/
    private Context mContext;
    private String gender = "男";
    private int type=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        type=bundle.getInt("type");
        initView();
        initData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopBar("安全设置", View.VISIBLE);
        if (type==1){
            //内部
            llyt_jobnum.setVisibility(View.VISIBLE);
        }else{
            //外部
            llyt_jobnum.setVisibility(View.GONE);
        }

    }

    /**
     * 初始界面设置
     */
    private void initData() {
    }

    @OnClick({R.id.iv_male,
            R.id.iv_woman,
            R.id.bt_submit})
    public void click(View view) {
        switch (view.getId()) {
            case R.id.iv_male:// 男
                gender = "男";
                iv_male.setBackgroundResource(R.drawable.ic_radio_selected);
                iv_woman.setBackgroundResource(R.drawable.ic_radio_default);
                break;
            case R.id.iv_woman://女
                iv_woman.setBackgroundResource(R.drawable.ic_radio_selected);
                iv_male.setBackgroundResource(R.drawable.ic_radio_default);
                gender = "女";
                break;
            case R.id.bt_submit://提交审核
                submit();
                break;

        }
    }


    /**
     * 提示消息
     *
     * @param str 需要提示的消息
     */
    private void toast(String str) {
        Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
    }

    private void submit() {
        /*获取用户输入的账号，密码*/
        String realName = et_name.getText().toString().trim();
        String idCard = et_identity_card.getText().toString().trim();
        String jobNumber = et_job_number.getText().toString().trim();
        String token = SpUser.getUserToken(mContext);

        /*条件过滤*/
        if (realName.isEmpty()) {
            //真实姓名不能为空
            toast("真实姓名不能为空");
            return;
        } else if (realName.length() <= 1) {
            //身份证号码不能为空
            toast("真实姓名长度至少2个字符");
            return;
        } else if (idCard.isEmpty()) {
            //身份证号码不能为空
            toast("身份证号码不能为空");
            return;
        }
        if (type==1){
            //内部
            if (jobNumber.isEmpty()) {
                toast("工号不能为空");
                return;
            }
        }

        /*判断身份证号*/
        String ss = "";
        try {
            ss = IdCheckUtils.IDCardValidate(idCard);
            if (!ss.equals("")) {
                CommonUtils.showToastMsg(mContext, ss);
                return;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        /*实名认证请求参数*/
        RequestParams params = new RequestParams();
        params.put("token", token);
        params.put("gender", gender);
        params.put("realName", realName);
        params.put("idCard", idCard);
        if (type==1){
            //内部
            params.put("memberNO", jobNumber);
        }
        params.put("memberType", type);

        /*提交实名认证请求*/
        submitRequest(params);
    }


    /**
     * 提交审核请求
     *
     * @param params 请求参数
     */
    private void submitRequest(RequestParams params) {
        String url = ConstantsServerUrl.getSaveUserAuthUrl();// 实名认证请求URL接口
        //获取验证码
        RegistResult submitResult = new RegistResult(responseResult);
        try {
            new HttpRequest(this).doPost(submitResult, url, params);
        } catch (ArgumentsException e) {
            Log.i("INFO", "1e:" + e);
            e.printStackTrace();
        } catch (RequestException e) {
            Log.i("INFO", "2e:" + e);
            e.printStackTrace();
        }
    }

    /**
     * 提交请求结果处理
     */
    AbstractResponseResult responseResult = new AbstractResponseResult() {

        @Override
        public void executeSuccess(Object object) {
            SpUser.setIdCardVStatus(mContext, 1);
            SpUser.setUserInfo(mContext, ConstantsCommon.USER_REALNAME, et_name.getText().toString());
            finish();
            toast("认证成功");
            if (type==1){
                SpUser.setUserJobNum(mContext,et_job_number.getText().toString().trim());
                SpUser.setUserType(mContext,1);
            }else {
                SpUser.setUserType(mContext,2);
            }
        }

        @Override
        public void executeError(String message, int errorCode) {
            CommonUtils.showToastMsg(context, "" + message);
        }

        @Override
        public void HttpError(String message, int errorCode) {
            CommonUtils.showToastMsg(context, "" + message);
        }
    };

}

